require 'View/UIBase/UIBase'

UISpecialDraw = UIBase.New(PanelResNames.UISpecialDraw)

function UISpecialDraw.Init(func, id, goFunc)
    UISpecialDraw.activeId = id;
    UISpecialDraw.goFunc = goFunc;
    UIBase.Init(UISpecialDraw,func);
end

-- 创建回调
function UISpecialDraw:OnCreate()
    self.countDownNum = 0;
    self.Btn_CardPool = self:GetChild("Btn_CardPool");
    self.Loader_CardPool = self:GetChild("Loader_CardPool", self.Btn_CardPool);
    self.Text_CardName = self:GetChild("Text_CardName", self.Btn_CardPool);
    self.Btn_Summon = self:GetChild("Btn_Summon", self.Btn_CardPool);
    self.Text_ContinueTimes = self:GetChild("Text_ContinueTimes", self.Btn_CardPool);
    self.Loader_Cost = self:GetChild("Loader_Cost", self.Btn_CardPool);
    self.Text_Num = self:GetChild("Text_Num", self.Btn_CardPool);
    self.Text_Buy = self:GetChild("Text_Buy", self.Btn_CardPool);
    self.Text_Tips = self:GetChild("Text_Tips", self.Btn_CardPool);
    self.Text_Time = self:GetChild("Text_Time", self.Btn_CardPool);
    self.ResBar = self:GetChild("ResBar");
    self.btnJump = self:GetChild("btnJump", self.ResBar);
    self.typeController = self:GetController("Type", self.Btn_CardPool);
    self.overController = self:GetController("IsOver", self.Btn_CardPool);
    self.getShowController=self:GetController("GetShow",self.Btn_CardPool)
    self.textDesc=self:GetChild("Text",self.Btn_CardPool)
    self.textDesc.text=getLanguage("ActivityDesc1040102")
    self.cntDnText=self:GetChild("Text_Time2",self.Btn_CardPool)
    self:addEvent();
end

-- 显示
function UISpecialDraw:OnEnable()
    if self.activeId == nil then
        self:OnBack();
    else
        self:Refresh();
    end
    self:UpdateCntDn()
    self:TimeCall(self.UpdateCntDn,1000,-1)
end

function UISpecialDraw:addEvent()
    self:RegisterOnClick(self:GetChild("Btn_Back"),self.OnClose);
    self:RegisterOnClick(self.btnJump,self.OnJump);
    self:RegisterOnClick(self:GetChild("Btn_Info", self.Btn_CardPool), self.OnHelp);
    self:RegisterOnClick(self:GetChild("Btn_Summon", self.Btn_CardPool), self.OnDraw);
    self:RegisterEvent(Config.EventType.Fresh_Active,self.Refresh);
    self:RegisterEvent(Config.EventType.Fresh_Currency,self.updateCost);
end

function UISpecialDraw:removeEvent()
end

function UISpecialDraw:Refresh()
    self:UpdateData();
end
function UISpecialDraw:UpdateCntDn()
    local _,serverData=logicMgr.ActiveManager. disposeSpecialDrawSeverData(self.activeId)
    local time=HelperFunc.GetServerTime()
    local activeState=0
    local endTime=-1
    local flag=false
    if not table.isNullOrEmpty(serverData)then
        local state=serverData.activeState
        if state==SpecialDrawState.waitToBuy then
            local cfg=G.dataTable["t_activity"][tostring(self.activeId)];
            endTime = serverData.startTime + (tonumber(cfg.f_ChallengeTime))-time;
            flag=true
        end
    end
    if not flag then
        return
    end
    self.cntDnText.text=string.format(getLanguage("ActivityDesc1040104"),HelperFunc.GetSurpTimeStr(endTime))
end
function UISpecialDraw:UpdateData()
    self.tableData, self.serverData = logicMgr.ActiveManager.getActiveData(self.activeId);
    if self.tableData == nil or self.serverData == nil then
        self:OnBack();
        return true;
    end
    self.Loader_CardPool.url = self.tableData.f_BackgroundPic;
    self.Text_CardName.text = getLanguage(self.tableData.f_PoolTitle);
    self.Text_ContinueTimes.text = string.format("%d/%d", self.tableData.f_10DrawTime - self.serverData.param.selectDrawTimes, self.tableData.f_10DrawTime);
    self.beginLimit = nil;
    local overState=0
    local activeState = self.serverData.activeState
    if activeState == SpecialDrawState.startDraw then
        self.typeController.selectedIndex = 0;
        self.Btn_Summon.title = getLanguage("OptionalPool_Btn_Start");
    elseif activeState == SpecialDrawState.continueDraw then
        self.typeController.selectedIndex = 1;
        self.Btn_Summon.title = getLanguage("OptionalPool_Btn_Continue");
    elseif activeState == SpecialDrawState.waitToBuy then
        self.typeController.selectedIndex = 4;
        self.Text_Buy.text = getLanguage("OptionalPool_Btn_Buy");
        self.getShowController.selectedIndex=1
    elseif activeState==SpecialDrawState.buy then
        self.typeController.selectedIndex = 2;
        self.Text_Buy.text = getLanguage("OptionalPool_Btn_Buy");
    elseif self.serverData.activeState == SpecialDrawState.locked then
        self.typeController.selectedIndex = 3;
        if self.tableData.f_OpenType == 1 then
            self.beginLimit = self.serverData.startTime + self.tableData.f_OpenParm - HelperFunc.GetServerTime();
            self.Text_Tips.text = string.format(getLanguage("Sys_OptionalPoolLocked1", nil, "%s"), HelperFunc.GetSurpTimeStr(self.beginLimit));
        elseif self.tableData.f_OpenType == 2 then
            local ficonfig = G.dataTable["t_stage"][tostring(self.tableData.f_OpenParm)];
            if ficonfig ~= nil then
                self.Text_Tips.text = string.format(getLanguage("Sys_OptionalPoolLocked2", nil, "%s"), ficonfig.f_ChapterId .. "-" .. ficonfig.f_StageNum)
            end
        elseif self.tableData.f_OpenType == 3 then
            self.beginLimit = logicMgr.ActiveManager.getDataUpateTime(logicMgr.HeroManager.GetRoleInfo().createTime) + self.tableData.f_OpenParm * 24 * 60 * 60 - HelperFunc.GetServerTime();
            self.Text_Tips.text = string.format(getLanguage("Sys_OptionalPoolLocked1", nil, "%s"), HelperFunc.GetSurpTimeStr(self.beginLimit));
        end
    elseif activeState==SpecialDrawState.haveBought then
        self.typeController.selectedIndex = 3;
        self.Text_Tips.text = getLanguage("OptionalPool_Finish");
        overState=1
    elseif activeState==SpecialDrawState.canNotBuy then
        self.typeController.selectedIndex = 3;
        self.Text_Tips.text = getLanguage("OptionalPool_Finish");
        overState=1
    end
    self.overController.selectedIndex=overState
    self:RemoveTimeCall(self.OnCountDown);
    if overState==0 then
        self.countDownNum = self.serverData.endTime - HelperFunc.GetServerTime();
        if self.countDownNum <= 0 then
            self:OnCountDown()
        else
            self.overController.selectedIndex = 0;
            self:TimeCall(self.OnCountDown, 1000, -1);
            self.Text_Time.text = string.format(getLanguage("Sys_ActivityTimeLeft"), HelperFunc.GetSurpTimeStr(self.countDownNum));
        end
    end
    self:updateCost();
end

function UISpecialDraw:updateCost()
    local cost = decodeJsonStrToTabel(self.tableData.f_Cost);
    cost = cost[1];
    local haveNum = logicMgr.ItemManager.getBagNumByTableID(cost[1]);
    self.Loader_Cost.url = EnumConst.AtlasPath.ItemAtlas .. G.dataTable["t_item"][tostring(cost[1])].f_Icon;
    if haveNum >= cost[2] then
        self.Text_Num.text = "[color=#FFFFFF]" .. cost[2] .. "[/color]";
    else
        self.Text_Num.text = "[color=#FF0000]" .. cost[2] .. "[/color]";
    end
    self.ResBar.icon = EnumConst.AtlasPath.ItemAtlas .. G.dataTable["t_item"][tostring(cost[1])].f_Icon;
    self.ResBar.title = haveNum;
    self.costId = cost[1];
end

--- 倒计时
function UISpecialDraw:OnCountDown()
    self.countDownNum = self.countDownNum - 1;
    self.Text_Time.text = string.format(getLanguage("Sys_ActivityTimeLeft"),HelperFunc.GetSurpTimeStr(self.countDownNum));
    if self.countDownNum <= 0 then
        self.overController.selectedIndex = 1;
        self.Text_Time.text = getLanguage("Sys_ActivityEnd");
    else
        self.overController.selectedIndex=0
    end
    if self.beginLimit ~= nil then
        self.beginLimit = self.beginLimit - 1;
        self.Text_Tips.text = string.format(getLanguage("Sys_OptionalPoolLocked1", nil, "%s"), HelperFunc.GetSurpTimeStr(self.beginLimit));
        if self.beginLimit <= 0 then
            self:UpdateData();
        end
    end
end

--- 抽奖
function UISpecialDraw:OnDraw()
    if self.countDownNum <= 0 then
        return
    end
    if self:UpdateData() then
        return ;
    end
    if self.serverData.activeState == SpecialDrawState.startDraw then
        -- 开始召唤
        drawHandler.GoReqSelectDrawRefreshMessage(self.activeId);
    elseif self.serverData.activeState == SpecialDrawState.continueDraw then
        -- 继续召唤
        if table.getn(self.serverData.param.selectDrawSaved) == 0 then
            drawHandler.GoReqSelectDrawRefreshMessage(self.activeId);
        else
            showUI(PanelNames.UIDrawResult, self.serverData.param.selectDrawSaved, 2, self.activeId);
        end
    elseif self.serverData.activeState ==SpecialDrawState.waitToBuy then
        if table.isNullOrEmpty(self.serverData.param.selectDrawBox)then
            drawHandler.GoReqSelectDrawSaveMessage(tonumber(self.activeId), 1);
        end
        showUI(PanelNames.UISpecialDrawRecord, nil, self.activeId, 1);
    elseif  self.serverData.activeState==SpecialDrawState.buy then
        local flag;
        if table.isNullOrEmpty(self.serverData.param.selectDrawBox)then
            flag = true;
            drawHandler.GoReqSelectDrawSaveMessage(tonumber(self.activeId), 1);
        end
        showUI(PanelNames.UISpecialDrawRecord, nil, self.activeId, 1, flag);
    end
end

--- 说明
function UISpecialDraw:OnHelp()
    logicMgr.UIShowManager.ShowCommonTips("HelpTitle34", "HelpDes34", "FunctionStory9003");
end

function UISpecialDraw:OnJump()
    showUI(PanelNames.UICommonSource, function()
        self:OnClose();
        if self.goFunc then
            self.goFunc();
        end
    end, { self.costId }, self.btnJump.parent);
end

-- 关闭
function UISpecialDraw:OnClose()
    self:OnBack();
    self:RemoveAllTimeCall();
    logicMgr.ActiveManager.showPopUpTips();
end

-- 隐藏
function UISpecialDraw:OnDisable()
end

-- 卸载
function UISpecialDraw:OnDestroy()
    self:removeEvent();
end

return UISpecialDraw